import  './loginStyle.less'
import { Form, Input, Button } from 'antd'
import { LoginApi,getMenuApi } from '../api/login'
import Login from "../Login/login"
//  import routes from "../routes/index"
import { useNavigate,useRoutes } from "react-router-dom"
import HomePage from "../views/homePage"
import { message } from 'antd'
export default() => {
  const navigate =useNavigate()
  const element =useRoutes([
    {
      path:'/homePage',
      element: <HomePage/>
  },
  {
    path:'/login',
    element: <Login/>
},
  ])
  const onFinish = (values) => {
    let params={
      username:values.username,
      password:values.password
    }
    LoginApi(params).then((res) => {
      if (res.status === 200) {
        localStorage.setItem("token",res.data.token)
        getMenuApi().then(re => {
          if (re.status === 200) {
           localStorage.setItem("routerlist",JSON.stringify(re.data))
           navigate(re.data[0].path,{
            replace:true,
          })
          message.success('登录成功');
          } else {
            message.success(`${res.mesg}`);
          }
      });
    
      } else {
        message.success(`${res.mesg}`);
      }
    })
  };

  const onFinishFailed = (errorInfo) => {
    console.log('Failed:', errorInfo);
  };

  return (
    <div className="login">
 <Form
      className="formLogin"
      name="basic"
      labelCol={{
        span: 8,
      }}
      wrapperCol={{
        span: 16,
      }}
      onFinish={onFinish}
      onFinishFailed={onFinishFailed}
      autoComplete="off"
    >
      <Form.Item
        label="Username"
        name="username"
        className="item"
        rules={[
          {
            required: true,
            message: '请输入账号名称',
          },
        ]}
      >
        <Input />
      </Form.Item>

      <Form.Item
        label="Password"
        name="password"
        className="item"
        rules={[
          {
            required: true,
            message: '请输入账号密码',
          },
        ]}
      >
        <Input.Password />
      </Form.Item>
      <Form.Item
        wrapperCol={{
          offset: 8,
          span: 16,
        }}
      >
        <Button type="primary" htmlType="submit">
          提交
        </Button>
      </Form.Item>
      {element}
    </Form>
    </div>
   
  );
};
